﻿Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Data.OracleClient
Imports MySql.Data

Public Class FabricaADONet

    Private _provider As String
    Private _connectionString As String

    Public Sub New(ByVal _connectionName As String)

        _provider = ConfigurationManager.ConnectionStrings(_connectionName).ProviderName
        _connectionString = ConfigurationManager.ConnectionStrings(_connectionName).ConnectionString

    End Sub

    Public Function GetConnection() As IDbConnection

        Select Case _provider
            Case "System.Data.SqlClient"
                Return New SqlConnection(_connectionString)
            Case "System.Data.OracleClient"
                Return New OracleConnection(_connectionString)
            Case "MySql.Data.MySqlClient"
                Return New MySqlClient.MySqlConnection(_connectionString)
            Case Else
                Throw New ParticaoEquivalenciaException("O provider " & _provider & " não é suportado.")
        End Select

    End Function

    Public Function GetCommand(ByVal _dbConnection As IDbConnection) As IDbCommand

        Dim _dbCommand As IDbCommand

        Select Case _provider
            Case "System.Data.SqlClient"
                _dbCommand = New SqlCommand
            Case "System.Data.OracleClient"
                _dbCommand = New OracleCommand
            Case "MySql.Data.MySqlClient"
                _dbCommand = New MySqlClient.MySqlCommand
            Case Else
                Throw New ParticaoEquivalenciaException("O provider " & _provider & " não é suportado.")
        End Select

        _dbCommand.Connection = _dbConnection
        Return _dbCommand

    End Function

    Public Function GetCommand(ByVal _SQL As String, ByVal _dbConnection As IDbConnection) As IDbCommand

        Dim _dbCommand As IDbCommand

        Select Case _provider
            Case "System.Data.SqlClient"
                _dbCommand = New SqlCommand
            Case "System.Data.OracleClient"
                _dbCommand = New OracleCommand
            Case "MySql.Data.MySqlClient"
                _dbCommand = New MySqlClient.MySqlCommand
            Case Else
                Throw New ParticaoEquivalenciaException("O provider " & _provider & " não é suportado.")
        End Select

        _dbCommand.Connection = _dbConnection
        _dbCommand.CommandText = _SQL

        Return _dbCommand

    End Function

End Class